package org.jboss.tools.examples.util;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class DataAccessController {

	public List findRange(Class clazz, int begin, int end, EntityManager em) {
		CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
		cq.select(cq.from(clazz));
		Query q = em.createQuery(cq);
		q.setMaxResults(end - begin);
		q.setFirstResult(begin);
		return q.getResultList();
	}

	public int count(Class clazz, EntityManager em) {
		CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
		Root rt = cq.from(clazz);
		cq.select(em.getCriteriaBuilder().count(rt));
		Query q = em.createQuery(cq);
		return ((Long) q.getSingleResult()).intValue();
	}
}
